﻿/**************************************************************************
创建日期:	2019/6/10
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述：
记	录：
    加入重置配置的方法，用于动态配置连接字符串 2022.5.7
***************************************************************************/
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Repository;
using log4net.Repository.Hierarchy;
using System;
using System.IO;
using ZhCun.Utils.Helpers;

namespace ZhCun.Utils
{
    public class LogHelper
    {
        static ILog _LogObj;

        static object _Object = new object();

        public static string LoggerName { set; get; }

        public static ILoggerRepository Repository { get; set; }

        public static ILog CreateLog(string loggerName)
        {
            if (Repository == null)
            {
                Repository = LogManager.CreateRepository("NETCoreRepository");
                XmlConfigurator.Configure(Repository, new FileInfo("log4net.config"));
            }
            ILog log = LogManager.GetLogger(Repository.Name, loggerName);
            return log;
        }
        /// <summary>
        /// 重置 log4net 配置,用于滞后设置连接字符串
        /// </summary>
        public static void ResetConfigure()
        {
            LogManager.ResetConfiguration("NETCoreRepository");
            XmlConfigurator.Configure(Repository, new FileInfo("log4net.config"));
            LogObj.Info("重置log4net配置完成");
        }
                
        public static ILog CreateLog()
        {
            if (string.IsNullOrWhiteSpace(LoggerName))
            {
                LoggerName = "Default";
            }
            return CreateLog(LoggerName);
        }

        public static ILog LogObj
        {
            get
            {
                lock (_Object)
                {
                    if (_LogObj == null)
                    {
                        _LogObj = CreateLog();
                    }
                    return _LogObj;
                }
            }
        }
    }
}